<?php

class Skjb_Rsync
{
    private $_path = '';
    
    private $_password = '';
    
    private $_source = '';
    
    private $_destination = '';
    
    private $_files = array();
    
    public function __construct($path, $password, $source, $destination)
    {
        $this->_path = $path;
        $this->_password = $password;
        $this->_source = $source;
        $this->_destination = $destination;
    }
    
    public function addFile($file)
    {
        $this->_files[] = $file;
    }
    
    public function sync()
    {
        $filename = tempnam('/tmp', 'Skjb_');
        
        $files = implode("\n", $this->_files);
        
        file_put_contents($filename, $files);
        
        $command = 'export RSYNC_PASSWORD=' . $this->_password . ';';
        $command .= $this->_path;
        $command .= ' -azv';
        $command .= ' --files-from="' . $filename . '"';
        $command .= ' ' . $this->_source;
        $command .= ' ' . $this->_destination;
        
        shell_exec($command);
        
        unlink($filename);
    }
}